package com.renwei.yunlong.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.renwei.yunlong.R;
import com.renwei.yunlong.bean.SponsorBean;
import com.renwei.yunlong.utils.ACache;
import com.zyf.fwms.commonlibrary.base.baseadapter.BaseRecyclerViewAdapter;
import com.renwei.yunlong.http.Api;
import com.zyf.fwms.commonlibrary.utils.GlideCircleTransform;

/**
 * 服务商  工单  发起人
 * <p>
 * Created by Administrator on 2017/9/13.
 */

public class PersonSelectAdapter extends BaseRecyclerViewAdapter<SponsorBean.Rows> {

    private String companyType;
    private boolean isShow;

    public PersonSelectAdapter(Context context) {
        super(context);
        companyType = ACache.get(context).getAsString("companyType");
    }

    public void setShow(boolean isShow){
        this.isShow =isShow;
    }

    @Override
    public SelectSponsorViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.item_select_sponsor, parent,false);
        return new SelectSponsorViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder rholder, final int position) {
        SelectSponsorViewHolder holder = (SelectSponsorViewHolder) rholder;

        final SponsorBean.Rows rows = mList.get(position);
        Glide.with(mContext).load("1".equals(companyType)? Api.$().OWNER_FILE_URL+rows.getAvatar():Api.$().SERVICE_FILE_URL+rows.getAvatar())
                .placeholder(R.mipmap.icon_header_default).error(R.mipmap.icon_header_default).transform(new GlideCircleTransform(mContext)).into(holder.ivHead);
        holder.tvName.setText(rows.getName());
        holder.tvDepartment.setText(rows.getDepartmentName());
        if (!TextUtils.isEmpty(rows.getUserGroupName())){
            holder.tvUsergroup.setText("/"+rows.getUserGroupName());
        }
        if (isShow){
            holder.llNumber.setVisibility(View.VISIBLE);
            holder.tvNumber.setText(rows.getWorkNum()+"");
        }else{
            holder.llNumber.setVisibility(View.GONE);
        }
        holder.tvPhone.setText(rows.getMobilePhone());
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mOnItemClickLitener.onItemClick(v,position);
            }
        });
    }

    class SelectSponsorViewHolder extends RecyclerView.ViewHolder {

        private View itemView;
        private ImageView ivHead;
        private TextView tvName;//名称
        private TextView tvDepartment;//部门
        private TextView tvUsergroup;//职能
        private TextView tvPhone;
        private TextView tvNumber;
        private LinearLayout llNumber;

        public SelectSponsorViewHolder(View view) {
            super(view);
            itemView = view;
            ivHead = itemView.findViewById(R.id.iv_head);
            tvName = itemView.findViewById(R.id.tv_name);
            tvDepartment = itemView.findViewById(R.id.tv_department);
            tvUsergroup = itemView.findViewById(R.id.tv_usergroup);
            tvPhone = itemView.findViewById(R.id.tv_phone);
            llNumber = itemView.findViewById(R.id.ll_number);
            tvNumber = itemView.findViewById(R.id.tv_number);
        }
    }
}
